package com.atguigu.yygh.hosp.controller;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Random;

/**
 * @ClassName HospitalSetController
 * @Description: 医院设置接口
 * @Author: admin
 * @CreateDate: 2024/5/25 17:25
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/5/25 17:25
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */

@Api(tags = "医院设置管理")
@RestController
//@CrossOrigin //解决跨域问题
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 查询所有医院设置信息
     * @return 返回所以数据
     */
    @ApiOperation(value = "获取所有医院设置")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> findAllHospitalSetList() {
        List<HospitalSet> list = hospitalSetService.list();
        Result<List<HospitalSet>> result = Result.ok(list);
        return result;
    }

    /**
     * 根据id查询医院设置信息
     * @param id 删除的id
     * @return 返回删除的结果
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result deleteHospitalSet(@PathVariable("id") String id) {
        if(hospitalSetService.removeById(id)){
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 分页查询医院设置信息
     * @param current 当前页
     * @param limit 每页显示条数
     * @param hospitalSetQueryVo 查询条件
     * @return 返回分页数据
     */
    @ApiOperation(value = "分页查询医院设置")
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHospSet(@PathVariable("current") Long current,
                                  @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {  //@RequestBody(required = false)  允许为空
        //分页
        Page<HospitalSet> page = new Page<>(current, limit);
        //条件查询
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        //值可能为空

        //hosname.isEmpty() 如果字符串为null，调用这个方法会抛出NullPointerException。
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname", hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode", hoscode);
        }
        //分页查询
        Page<HospitalSet> result = hospitalSetService.page(page, wrapper);

        return Result.ok(result);
    }

    /**
     * 添加医院设置信息
     * @param hospitalSet 医院设置信息
     * @return 返回添加的结果
     */
    @ApiOperation(value = "添加医院设置信息")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态1为启用，0不能用
        hospitalSet.setStatus(1);
        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据id获取医院设置信息
     * @param id 医院设置id
     * @return 返回医院设置信息
     */
    @ApiOperation(value = "根据id获取医院设置信息")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") Long id){
        //模拟异常
//        try {
//            System.out.println(1/0);
//        } catch (Exception e) {
//            throw new YyghException("失败",201);
//        }
        HospitalSet result = hospitalSetService.getById(id);
        return Result.ok(result);
    }

    /**
     * 修改医院设置信息
     * @param hospitalSet 医院设置信息
     * @return 返回修改的结果
     */
    @ApiOperation(value = "修改医院设置")
    @PutMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        if(hospitalSetService.updateById(hospitalSet)){
            return Result.ok();
        }
        return Result.fail();
    }


    /**
     * 批量删除医院设置信息
     * @param idList 医院设置id列表
     * @return 返回删除的结果
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 医院设置锁定和解锁
     * @param id 医院设置id
     * @param status 状态 1为启用，0为禁用
     * @return 返回修改的结果
     */
    @ApiOperation(value = "医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id,
                                  @PathVariable("status") Integer status){
        //根据id查询医院设置
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //修改状态
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    /**
     * 发送签名密钥，展示尚未完善
     * @param id 医院设置id
     * @return 返回发送结果
     */
    @ApiOperation(value = "发送签名密钥")
    @GetMapping("sendKey/{id}")
    public Result sendKey(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信进行完善
        return Result.ok();
    }

}
